local debug = require "debug"
-- require 'luaCPPSTD'
-- require "luaQGallop"
require "Q3DViewer"
require "luaQt"
require "QGMainWindow"
package.loaded["MPIPond"]=nil;
package.loaded["RCS"]=nil;
package.loaded["PSO"]=nil;
package.loaded["LDPSO"]=nil;
-- package.loaded["luaRCWA"]=nil;
-- package.loaded["RCWAHelp"]=nil;

assert( filepath, "The input path is invalid!" )


View3D={}
function View3D.perspective()
	local mw = QGallop.getMainWindow();
	if mw~=nill then
		local view = mw:get3DView();
		if view~=nil then
			view:perspective();
		end
	end
end

local f = assert(loadfile(filepath))

function trace (event, line)
    if GetStopFlag()==true then
        local s = debug.getinfo(2).source
        -- print( s .. ":" .. line )
	--error("Stop solver")
	print("Stop solver")
    end
end
debug.sethook(trace, "l")

-- function traceback(err)
	-- print("LUA ERROR: " .. tostring(err))
	-- print(debug.traceback())
-- end

local errStr="";
local errCode="";

function traceback(err)
   if  GetStopFlag()==true then
	return   
   end;
   for level = 1, math.huge do
        local info = debug.getinfo(level, "nSlf")
        if not info then break end
        if info.what == "C" then -- is a C function?
            print(level, "C function")
        else
            print(string.format("%d\t[%s] : %d", level, info.source, info.currentline))
        end
    end
    print("LUA ERROR: " .. tostring(err))
    errStr = err;
    for  level = 2, math.huge do
        local info = debug.getinfo(level, "nSlf")
	-- print("info:",info)
        if not info then break end
	 local source = info.source
         if info.what ~= "C" then -- is a C function?
	     longpath = string.match(source, "@(.+)")
	     path,linenum,msg = string.match(err, "(.+):([0-9]+):(.+)")
	     print(longpath)
	     if longpath~=nil then
		 errStr = (longpath)..":"..(info.currentline)..":".. ( msg or (" "..err) )
		 break;
	     end
	  end
    end
end 
 

local status = xpcall(f, traceback)
debug.sethook()

if errStr ~= "" then
    -- print ("Error:", errStr)
    error(errStr)
end

